package net.ibizsys.eclipse.studio.handler;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import net.ibizsys.eclipse.studio.Activator;
import net.ibizsys.eclipse.studio.console.Console;
import net.ibizsys.eclipse.studio.i18n.I18n;
import net.ibizsys.eclipse.studio.project.BaseBatchBuilder;
import net.ibizsys.eclipse.studio.util.SystemKit;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * 合并系统代码
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class MergeSystemHandler extends AbstractHandler
{
	public MergeSystemHandler()
	{
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException
	{
		final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);

		if (window != null)
		{
			IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
			Object firstElement = selection.getFirstElement();
			if (firstElement instanceof IAdaptable)
			{
				IProject project = (IProject) ((IAdaptable) firstElement).getAdapter(IProject.class);

				String mergeBat = Activator.getDefault().getRootPath() + File.separator + project.getName() + File.separator;

				if (SystemKit.isWindows())
				{
					mergeBat += BaseBatchBuilder.WinBatchFileName;
				} else
				{
					mergeBat += BaseBatchBuilder.LnxBatchFileName;
				}

				File file = new File(mergeBat);

				if (!file.exists())
				{
					MessageDialog.openError(window.getShell(), I18n.Error, I18n.BatchFileNotFoundError);
					return null;
				}

				if (SystemKit.isWindows())
				{
					try
					{
						Runtime.getRuntime().exec("cmd /c start " + mergeBat);
					} catch (Exception e)
					{
						MessageDialog.openError(window.getShell(), I18n.Error, I18n.BatchExecuteError);
					}
				} else
				{
					final String bat = mergeBat;
					new Thread()
					{
						@Override
						public void run()
						{
							try
							{
								Runtime.getRuntime().exec("chmod +x " + bat).waitFor();

								Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", bat });
								process.waitFor();

								InputStream in = process.getInputStream();
								BufferedReader read = new BufferedReader(new InputStreamReader(in));
								String line = null;
								while ((line = read.readLine()) != null)
								{
									Console.println(line);
								}

								Console.println("合并项目结束.");
							} catch (Exception e)
							{
								MessageDialog.openError(window.getShell(), I18n.Error, I18n.BatchExecuteError);
							}

						}
					}.start();

				}
			}
		}

		return null;
	}
}